//
//  TitleButton.swift
//  SinaWeibo
//
//  Created by 胡阳 on 16/5/20.
//  Copyright © 2016年 young4ever. All rights reserved.
//

import UIKit

class TitleButton: UIButton {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        setImage(UIImage(named: "navigationbar_arrow_down"), forState: UIControlState.Normal)
        setImage(UIImage(named: "navigationbar_arrow_up"), forState: UIControlState.Selected)
        sizeToFit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 通过print发现layoutSubviews这个方法系统调用了2次, 导致结果不正确 , 乘以0.5就可以了<这种做法不优雅,不推荐使用>
//        print("~~~~~~~~~~~~~")
//        titleLabel?.frame.offsetInPlace(dx: -(imageView!.bounds.size.width * 0.5), dy: 0)
//        imageView?.frame.offsetInPlace(dx: titleLabel!.bounds.size.width * 0.5, dy: 0)
        
        titleLabel?.frame.origin.x = 0
        imageView?.frame.origin.x = titleLabel!.frame.size.width
    }

}
